{% extends "public/base.html" %}

{% block title %}{{ publication.custom_title or scale.title }} - 测评结果{% endblock %}

{% block extra_css %}
<style>
.result-container {
    max-width: 900px;
    margin: 0 auto;
}

.result-header {
    background: linear-gradient(135deg, #007bff 0%, #0056b3 100%);
    color: white;
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    text-align: center;
    position: relative;
    overflow: hidden;
}

.result-header::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
    animation: shimmer 3s ease-in-out infinite;
}

@keyframes shimmer {
    0%, 100% { transform: rotate(0deg); }
    50% { transform: rotate(180deg); }
}

.result-header h1 {
    font-size: 2.5rem;
    font-weight: bold;
    margin-bottom: 0.5rem;
    position: relative;
    z-index: 1;
}

.result-header .subtitle {
    font-size: 1.1rem;
    opacity: 0.9;
    position: relative;
    z-index: 1;
}

.completion-badge {
    position: absolute;
    top: 1rem;
    right: 1rem;
    background: rgba(255,255,255,0.2);
    border: 2px solid rgba(255,255,255,0.3);
    border-radius: 50px;
    padding: 0.5rem 1rem;
    font-weight: bold;
    z-index: 1;
}

.score-section {
    background: white;
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    text-align: center;
}

.score-circle {
    width: 200px;
    height: 200px;
    border-radius: 50%;
    background: conic-gradient(from 0deg, #28a745 0deg, #28a745 var(--score-angle), #e9ecef var(--score-angle), #e9ecef 360deg);
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 1.5rem;
    position: relative;
}

.score-circle::before {
    content: '';
    position: absolute;
    width: 160px;
    height: 160px;
    background: white;
    border-radius: 50%;
}

.score-value {
    position: relative;
    z-index: 1;
    font-size: 3rem;
    font-weight: bold;
    color: #333;
}

.score-label {
    font-size: 1.1rem;
    color: #6c757d;
    margin-bottom: 1rem;
}

.score-description {
    font-size: 1rem;
    color: #495057;
}

.result-level {
    background: white;
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.level-badge {
    display: inline-block;
    padding: 0.75rem 1.5rem;
    border-radius: 25px;
    font-weight: bold;
    font-size: 1.1rem;
    margin-bottom: 1rem;
}

.level-normal {
    background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
    color: white;
}

.level-mild {
    background: linear-gradient(135deg, #ffc107 0%, #fd7e14 100%);
    color: white;
}

.level-moderate {
    background: linear-gradient(135deg, #fd7e14 0%, #dc3545 100%);
    color: white;
}

.level-severe {
    background: linear-gradient(135deg, #dc3545 0%, #6f42c1 100%);
    color: white;
}

.result-description {
    font-size: 1.1rem;
    line-height: 1.6;
    color: #495057;
    margin-bottom: 1.5rem;
}

.suggestions-section {
    background: white;
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.suggestion-item {
    background: #f8f9fa;
    border-left: 4px solid #007bff;
    border-radius: 8px;
    padding: 1rem 1.5rem;
    margin-bottom: 1rem;
    transition: all 0.3s ease;
}

.suggestion-item:hover {
    background: #e9ecef;
    transform: translateX(5px);
}

.suggestion-item:last-child {
    margin-bottom: 0;
}

.suggestion-icon {
    color: #007bff;
    margin-right: 0.5rem;
}

.dimensions-section {
    background: white;
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.dimension-item {
    margin-bottom: 1.5rem;
    padding-bottom: 1.5rem;
    border-bottom: 1px solid #e9ecef;
}

.dimension-item:last-child {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
}

.dimension-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}

.dimension-name {
    font-weight: 600;
    color: #333;
}

.dimension-score {
    font-weight: bold;
    color: #007bff;
}

.dimension-bar {
    height: 8px;
    background: #e9ecef;
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 0.5rem;
}

.dimension-progress {
    height: 100%;
    background: linear-gradient(90deg, #007bff 0%, #0056b3 100%);
    border-radius: 4px;
    transition: width 1s ease;
}

.dimension-description {
    font-size: 0.9rem;
    color: #6c757d;
    line-height: 1.4;
}

.stats-section {
    background: white;
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}

.stat-item {
    text-align: center;
    padding: 1rem;
}

.stat-value {
    font-size: 2rem;
    font-weight: bold;
    color: #007bff;
    margin-bottom: 0.5rem;
}

.stat-label {
    font-size: 0.9rem;
    color: #6c757d;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.actions-section {
    background: white;
    border-radius: 15px;
    padding: 2rem;
    margin-bottom: 2rem;
    box-shadow: 0 4px 15px rgba(0,0,0,0.1);
    text-align: center;
}

.action-button {
    margin: 0.5rem;
    padding: 0.75rem 1.5rem;
    border-radius: 25px;
    font-weight: 500;
    transition: all 0.3s ease;
    text-decoration: none;
    display: inline-block;
}

.action-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    text-decoration: none;
}

.btn-share {
    background: linear-gradient(135deg, #17a2b8 0%, #138496 100%);
    color: white;
    border: none;
}

.btn-download {
    background: linear-gradient(135deg, #28a745 0%, #20c997 100());
    color: white;
    border: none;
}

.btn-retake {
    background: linear-gradient(135deg, #6c757d 0%, #495057 100%);
    color: white;
    border: none;
}

.disclaimer {
    background: #fff3cd;
    border: 1px solid #ffeaa7;
    border-radius: 10px;
    padding: 1.5rem;
    margin-bottom: 2rem;
}

.disclaimer-title {
    font-weight: bold;
    color: #856404;
    margin-bottom: 0.5rem;
}

.disclaimer-text {
    color: #856404;
    font-size: 0.9rem;
    line-height: 1.5;
    margin: 0;
}

.publisher-info {
    background: #f8f9fa;
    border-radius: 10px;
    padding: 1.5rem;
    margin-top: 2rem;
    text-align: center;
}

.publisher-info h6 {
    color: #495057;
    margin-bottom: 0.5rem;
}

.publisher-info p {
    color: #6c757d;
    font-size: 0.9rem;
    margin: 0;
}

@media (max-width: 768px) {
    .result-container {
        padding: 0 1rem;
    }
    
    .result-header {
        padding: 1.5rem;
    }
    
    .result-header h1 {
        font-size: 2rem;
    }
    
    .score-circle {
        width: 150px;
        height: 150px;
    }
    
    .score-circle::before {
        width: 120px;
        height: 120px;
    }
    
    .score-value {
        font-size: 2.5rem;
    }
    
    .action-button {
        display: block;
        margin: 0.5rem 0;
        width: 100%;
    }
}

.fade-in {
    animation: fadeIn 0.8s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.slide-in {
    animation: slideIn 1s ease-out;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(-30px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}
</style>
{% endblock %}

{% block content %}
<div class="result-container">
    <!-- 结果头部 -->
    <div class="result-header fade-in">
        <div class="completion-badge">
            <i class="bi bi-check-circle me-1"></i>
            已完成
        </div>
        <h1>{{ publication.custom_title or scale.title }}</h1>
        <p class="subtitle">您的测评已完成，以下是详细分析结果</p>
    </div>

    <!-- 总分展示 -->
    <div class="score-section fade-in">
        <h3 class="mb-4">
            <i class="bi bi-trophy me-2"></i>
            总体得分
        </h3>
        <div class="score-circle" style="--score-angle: {{ (result.total_score / result.max_score * 360) if result.max_score > 0 else 0 }}deg;">
            <div class="score-value">{{ result.total_score }}</div>
        </div>
        <div class="score-label">总分：{{ result.total_score }} / {{ result.max_score }}</div>
        <div class="score-description">完成率：{{ "%.1f" | format(100.0 if assessment.total_questions > 0 else 0) }}%</div>
    </div>

    <!-- 结果等级 -->
    {% if result.level %}
    <div class="result-level slide-in">
        <h3 class="mb-3">
            <i class="bi bi-bar-chart me-2"></i>
            评估结果
        </h3>
        <div class="level-badge level-{{ result.level_class }}">
            {{ result.level }}
        </div>
        <div class="result-description">
            {{ result.description }}
        </div>
    </div>
    {% endif %}

    <!-- 维度分析 -->
    {% if result.dimensions %}
    <div class="dimensions-section slide-in">
        <h3 class="mb-4">
            <i class="bi bi-pie-chart me-2"></i>
            维度分析
        </h3>
        {% for dimension in result.dimensions %}
        <div class="dimension-item">
            <div class="dimension-header">
                <span class="dimension-name">{{ dimension.name }}</span>
                <span class="dimension-score">{{ dimension.score }} / {{ dimension.max_score }}</span>
            </div>
            <div class="dimension-bar">
                <div class="dimension-progress" 
                     style="width: {{ (dimension.score / dimension.max_score * 100) if dimension.max_score > 0 else 0 }}%;"></div>
            </div>
            {% if dimension.description %}
            <div class="dimension-description">{{ dimension.description }}</div>
            {% endif %}
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <!-- 建议和指导 -->
    {% if result.suggestions %}
    <div class="suggestions-section slide-in">
        <h3 class="mb-4">
            <i class="bi bi-lightbulb me-2"></i>
            建议与指导
        </h3>
        {% for suggestion in result.suggestions %}
        <div class="suggestion-item">
            <i class="bi bi-arrow-right suggestion-icon"></i>
            {{ suggestion }}
        </div>
        {% endfor %}
    </div>
    {% endif %}

    <!-- 测评统计 -->
    <div class="stats-section slide-in">
        <h3 class="mb-4 text-center">
            <i class="bi bi-graph-up me-2"></i>
            测评统计
        </h3>
        <div class="row">
            <div class="col-md-3 col-6">
                <div class="stat-item">
                    <div class="stat-value">{{ assessment.total_questions }}</div>
                    <div class="stat-label">已答题目</div>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="stat-item">
                    <div class="stat-value">{{ assessment.total_questions }}</div>
                    <div class="stat-label">总题目数</div>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="stat-item">
                    <div class="stat-value">{{ format_duration((assessment.completed_at - assessment.started_at).total_seconds() if assessment.completed_at and assessment.started_at else 0) }}</div>
                    <div class="stat-label">用时</div>
                </div>
            </div>
            <div class="col-md-3 col-6">
                <div class="stat-item">
                    <div class="stat-value">{{ assessment.completed_at.strftime('%m-%d') }}</div>
                    <div class="stat-label">完成日期</div>
                </div>
            </div>
        </div>
    </div>

    <!-- 操作按钮 -->
    <div class="actions-section slide-in">
        <h3 class="mb-4">
            <i class="bi bi-gear me-2"></i>
            更多操作
        </h3>
        <button class="btn btn-share action-button" onclick="shareResult()">
            <i class="bi bi-share me-2"></i>
            分享结果
        </button>
        <button class="btn btn-download action-button" onclick="downloadResult()">
            <i class="bi bi-download me-2"></i>
            下载报告
        </button>
        <a href="/public/scale/{{ publication.publication_token }}" class="btn btn-retake action-button">
            <i class="bi bi-arrow-clockwise me-2"></i>
            重新测评
        </a>
    </div>

    <!-- 免责声明 -->
    <div class="disclaimer slide-in">
        <div class="disclaimer-title">
            <i class="bi bi-info-circle me-2"></i>
            重要提示
        </div>
        <p class="disclaimer-text">
            本测评结果仅供参考，不能替代专业的心理咨询或医学诊断。如果您对结果有疑问或需要专业帮助，建议咨询相关专业人士。测评结果的准确性取决于您回答的真实性和完整性。
        </p>
    </div>

    <!-- 发布者信息 -->
    {% if publication.custom_description %}
    <div class="publisher-info slide-in">
        <h6>
            <i class="bi bi-person-badge me-2"></i>
            发布者说明
        </h6>
        <p>{{ publication.custom_description }}</p>
    </div>
    {% endif %}
</div>

<!-- 分享模态框 -->
<div class="modal fade" id="shareModal" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <i class="bi bi-share me-2"></i>
                    分享测评结果
                </h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="text-center mb-3">
                    <div class="share-preview">
                        <h6>{{ publication.custom_title or scale.title }}</h6>
                        <p class="text-muted">我的测评得分：{{ result.total_score }} / {{ result.max_score }}</p>
                        <p class="text-muted">评估结果：{{ result.level or '已完成' }}</p>
                    </div>
                </div>
                
                <div class="share-options">
                    <div class="row text-center">
                        <div class="col-4">
                            <button class="btn btn-outline-primary w-100" onclick="shareToWeChat()">
                                <i class="bi bi-wechat d-block mb-1"></i>
                                微信
                            </button>
                        </div>
                        <div class="col-4">
                            <button class="btn btn-outline-info w-100" onclick="shareToWeibo()">
                                <i class="bi bi-share d-block mb-1"></i>
                                微博
                            </button>
                        </div>
                        <div class="col-4">
                            <button class="btn btn-outline-success w-100" onclick="copyShareLink()">
                                <i class="bi bi-link d-block mb-1"></i>
                                复制链接
                            </button>
                        </div>
                    </div>
                </div>
                
                <div class="mt-3">
                    <label class="form-label">分享链接</label>
                    <div class="input-group">
                        <input type="text" class="form-control" id="share-link" 
                               value="{{ request.url }}" readonly>
                        <button class="btn btn-outline-secondary" onclick="copyShareLink()">
                            <i class="bi bi-clipboard"></i>
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
// 页面加载动画
document.addEventListener('DOMContentLoaded', function() {
    // 延迟显示各个部分
    const sections = document.querySelectorAll('.slide-in');
    sections.forEach((section, index) => {
        setTimeout(() => {
            section.style.animationDelay = `${index * 0.2}s`;
        }, 100);
    });
    
    // 动画进度条
    setTimeout(() => {
        const progressBars = document.querySelectorAll('.dimension-progress');
        progressBars.forEach(bar => {
            const width = bar.style.width;
            bar.style.width = '0%';
            setTimeout(() => {
                bar.style.width = width;
            }, 500);
        });
    }, 1000);
});

// 分享结果
function shareResult() {
    const modal = new bootstrap.Modal(document.getElementById('shareModal'));
    modal.show();
}

// 下载结果
function downloadResult() {
    showLoading('正在生成PDF报告...');
    
    // 创建下载链接
    const assessmentId = {{ assessment.id }};
    const downloadUrl = `/public/assessment/${assessmentId}/download`;
    
    // 创建隐藏的下载链接
    const link = document.createElement('a');
    link.href = downloadUrl;
    link.download = `{{ publication.custom_title or scale.title }}_测评报告.pdf`;
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    
    hideLoading();
    showResult(true, '报告下载已开始');
}

// 分享到微信
function shareToWeChat() {
    // 这里可以集成微信分享SDK
    copyShareLink();
    showResult(true, '链接已复制，请在微信中粘贴分享');
}

// 分享到微博
function shareToWeibo() {
    const text = `我刚完成了《{{ publication.custom_title or scale.title }}》测评，得分：{{ result.total_score }}/{{ result.max_score }}，评估结果：{{ result.level or '已完成' }}。`;
    const url = encodeURIComponent(window.location.href);
    const shareUrl = `https://service.weibo.com/share/share.php?url=${url}&title=${encodeURIComponent(text)}`;
    window.open(shareUrl, '_blank', 'width=600,height=400');
}

// 复制分享链接
function copyShareLink() {
    const shareLink = document.getElementById('share-link');
    shareLink.select();
    shareLink.setSelectionRange(0, 99999);
    
    try {
        document.execCommand('copy');
        showResult(true, '链接已复制到剪贴板');
        
        // 关闭模态框
        const modal = bootstrap.Modal.getInstance(document.getElementById('shareModal'));
        if (modal) {
            modal.hide();
        }
    } catch (err) {
        console.error('复制失败:', err);
        showResult(false, '复制失败，请手动复制链接');
    }
}

// 格式化时长
function formatDuration(seconds) {
    const minutes = Math.floor(seconds / 60);
    const remainingSeconds = seconds % 60;
    return `${minutes}分${remainingSeconds}秒`;
}

// 平滑滚动到顶部
function scrollToTop() {
    window.scrollTo({
        top: 0,
        behavior: 'smooth'
    });
}

// 页面滚动时显示返回顶部按钮
window.addEventListener('scroll', function() {
    const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
    const backToTop = document.getElementById('back-to-top');
    
    if (scrollTop > 300) {
        if (!backToTop) {
            createBackToTopButton();
        }
    } else {
        if (backToTop) {
            backToTop.remove();
        }
    }
});

// 创建返回顶部按钮
function createBackToTopButton() {
    const button = document.createElement('button');
    button.id = 'back-to-top';
    button.className = 'btn btn-primary';
    button.style.cssText = `
        position: fixed;
        bottom: 20px;
        right: 20px;
        width: 50px;
        height: 50px;
        border-radius: 50%;
        z-index: 1000;
        box-shadow: 0 4px 15px rgba(0,0,0,0.2);
    `;
    button.innerHTML = '<i class="bi bi-arrow-up"></i>';
    button.onclick = scrollToTop;
    document.body.appendChild(button);
}

// 键盘快捷键
document.addEventListener('keydown', function(event) {
    if (event.key === 'Escape') {
        // 关闭所有模态框
        const modals = document.querySelectorAll('.modal.show');
        modals.forEach(modal => {
            const modalInstance = bootstrap.Modal.getInstance(modal);
            if (modalInstance) {
                modalInstance.hide();
            }
        });
    } else if (event.key === 's' && event.ctrlKey) {
        // Ctrl+S 下载报告
        event.preventDefault();
        downloadResult();
    } else if (event.key === 'r' && event.ctrlKey) {
        // Ctrl+R 重新测评
        event.preventDefault();
        window.location.href = `/public/scale/{{ publication.publication_token }}`;
    }
});

// 打印样式优化
window.addEventListener('beforeprint', function() {
    // 隐藏不需要打印的元素
    const hideElements = document.querySelectorAll('.actions-section, .modal, #back-to-top');
    hideElements.forEach(el => {
        el.style.display = 'none';
    });
});

window.addEventListener('afterprint', function() {
    // 恢复隐藏的元素
    const hideElements = document.querySelectorAll('.actions-section, .modal');
    hideElements.forEach(el => {
        el.style.display = '';
    });
});
</script>
{% endblock %}